<?php
namespace app\material\controller;
use app\AdminRest;


use app\ApiRest;
use app\card\model\CardCount;
use app\card\model\Company;
use app\card\model\Job;
use app\card\model\UserInfo;
use app\material\model\Brochure;
use app\material\model\BrochureIm;
use app\material\model\Config;
use app\material\model\File;
use app\material\model\FileCollect;
use app\material\model\FileCompany;
use app\material\model\Folder;
use app\material\model\ImPage;
use app\material\model\ImVideo;
use app\shop\model\IndexUserInfo;
use think\App;
use think\facade\Db;


class IndexFile extends ApiRest
{

    protected $model;

    protected $brochure_model;

    protected $file_model;

    protected $folder_model;

    protected $config_model;

    protected $collect_model;

    protected $store_id;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new File();

        $this->brochure_model  = new Brochure();

        $this->file_model = new File();

        $this->folder_model  = new Folder();

        $this->config_model = new Config();

        $this->collect_model = new FileCollect();

        $user_model = new UserInfo();

        $this->store_id = $user_model->where(['fans_id'=>$this->getUserId()])->value('company_id');

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 14:38
     * @功能说明:智能文件夹首页
     */
    public function index(){

        $data = $this->config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $dis = [

            'user_id' => $this->getUserId(),

            'status'  => 1,

            'uniacid' => $this->_uniacid
        ];

        $user_folder = $this->folder_model->where($dis)->column('id');

        $user_folder[] = 0;
        //已经使用的大小
        $data['use_max'] = $this->file_model->where($dis)->where('file_type','<>',6)->where('folder_id','in',$user_folder)->sum('file_max');

        $data['use_max'] = getRangeMemKb(0,$data['use_max']);

        $data['user_upload_limit'] = getRangeMemKb(0,$data['user_upload_limit']*1024*1024);
        //收藏文件数量
        $data['collect_count'] = $this->collect_model->collectCount($this->getUserId());

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-06-24 16:15
     * @功能说明:企业文件文件夹列表
     */
    public function companyFolderList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        $dis[] = ['pid','=',0];

        $dis[] = ['user_id','=',0];

        if(!empty($input['name'])){

            $dis[] = ['title','like','%'.$input['name'].'%'];
        }

        $data = $this->folder_model->companyDataList($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 15:47
     * @功能说明:我的文件
     */
    public function myFileList(){

        $input = $this->_param;

        $dis = [

            'user_id' => $this->getUserId(),

            'pid'     => $input['folder_id'],

        ];


        $where = [

            'user_id'  => $this->getUserId(),

            'folder_id'=> $input['folder_id']
        ];

        $data = $this->file_model->folderOrFileList($where,$dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-01 18:01
     * @功能说明:文件夹列表
     */
    public function folderList(){

        $input = $this->_param;

        $dis = [

            'user_id' => $this->getUserId(),

            'pid'     => $input['folder_id']
        ];

        $data = $this->folder_model->where($dis)->order('top desc,id desc')->paginate(10)->toArray();

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 16:53
     * @功能说明:收藏列表
     */
    public function collectList(){

        $input = $this->_param;

        $dis[] = ['a.user_id','=',$this->getUserId()];

        $dis[] = ['b.status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['b.title','like','%'.$input['title'].'%'];
        }

        $data = $this->collect_model->dataList($dis);

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['file_max'] = getRangeMemKb(0,$v['file_max']);

            }
        }

        return $this->success($data);

    }






    /**
     * @author chenniang
     * @DataTime: 2021-06-24 16:22
     * @功能说明:添加文件夹
     */
    public function folderAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['type']    = 1;

        $res = $this->folder_model->dataAdd($input);

        return $this->success($res);



    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-24 16:38
     * @功能说明:编辑文件夹
     */
    public function folderUpdate(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $dis = [

          'id' => $input['id']
        ];

        Db::startTrans();

        $res = $this->folder_model->dataUpdate($dis,$input);
        //下架和删除会影响下级
        if(isset($input['status'])&&in_array($input['status'],[-1,0])){

            $this->file_model->dataUpdate(['user_id'=>$this->getUserId(),'folder_id'=>$input['id']],['status'=>$input['status']]);

            $folder_id = $this->folder_model->getLowId($input['id']);
        }
        //上架会影响上级
        if(isset($input['status'])&&in_array($input['status'],[1])){

            $folder_id = $this->folder_model->reset($input['id']);

        }

        if(!empty($folder_id)){

            $where[] = ['status','>',-1];

            $where[] = ['id','in',$folder_id];

            $this->folder_model->dataUpdate($where,['status'=>$input['status']]);

        }

        if(empty($input['status'])){

            $info  = $this->folder_model->dataInfo($dis);

            $top   = $this->folder_model->reset($info['pid']);

            $count = count($top);

            if($count>6){

                Db::rollback();

                $this->errorMsg('文件夹最多5级');
            }
        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-25 10:35
     * @功能说明:文件夹详情
     */
    public function folderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->folder_model->dataInfo($dis);

        return $this->success($res);
    }



    /**
     * @author chenniang
     * @DataTime: 2021-06-24 15:51
     * @功能说明:添加文件
     */
    public function fileAdd(){

        $input = $this->_input;

        $dis['uniacid'] = $this->_uniacid;

        $dis['user_id'] = $this->getUserId();

        $dis['status']  = 1;

        $input['uniacid'] = $this->_uniacid;

        $input['user_id'] = $this->getUserId();

        $input['status']  = 1;

        $user_folder = $this->folder_model->where($dis)->column('id');

        $user_folder[] = 0;
        //已经使用多少
        $have_max = $this->file_model->where($dis)->where('file_type','<>',6)->where('folder_id','in',$user_folder)->sum('file_max');

        $data = $this->config_model->dataInfo(['uniacid'=>$this->_uniacid]);

        $user_upload_limit = $data['user_upload_limit'];

        if(isset($input['file_max'])){

            $have_max+=$input['file_max'];
        }


        if($have_max>$user_upload_limit*1024*1024){

            $this->errorMsg('超过上传限制');
        }

        $res = $this->file_model->dataAdd($input);

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-06-24 15:51
     * @功能说明:编辑文件
     */
    public function fileUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        $res = $this->file_model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-25 10:35
     * @功能说明:文件详情
     */
    public function fileInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $res = $this->file_model->dataInfo($dis);

        $dis = [

            'file_id' => $res['id'],

            'user_id' => $this->getUserId()
        ];

        $collect = $this->collect_model->dataInfo($dis);

        $res['is_collect'] = !empty($collect)?1:0;

        if(!empty($input['staff_id'])){

            $staff['name']   = lbUserName($input['staff_id']);

            $staff['avatar'] = lbUserAvatar($input['staff_id']);

            $staff['company'] = lbUserCompany($input['staff_id']);

            $user_model = new IndexUserInfo();

            $job_model  = new Job();

            $user = $user_model->userInfo(['fans_id'=>$input['staff_id']]);

            if(!empty($user)){

                $staff['mobile'] = $user['phone'];

                $staff['job'] = $job_model->where(['id'=>$user['job_id']])->value('name');

                $staff['job'] = !empty($staff['job'])?$staff['job']:'';

            }


            $res['staff_info'] = $staff;

        }

        return $this->success($res);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-30 10:28
     * @功能说明:收藏取消收藏
     */
    public function collectFile(){

        $input = $this->_param;

        $dis = [

            'file_id' => $input['file_id'],

            'user_id' => $this->getUserId(),

            'uniacid' => $this->_uniacid

        ];

        $info = $this->collect_model->dataInfo($dis);

        if(empty($info)){

            $res = $this->collect_model->dataAdd($dis);

        }else{

            $res = $this->collect_model->where($dis)->delete();
        }

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-29 15:47
     * @功能说明:文件夹列表
     */
    public function fileAndFolderList(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'pid'     => $input['folder_id'],

            'status'  => 1

        ];

        $company_model = new FileCompany();

        $file_id = $company_model->where(['company_id'=>$this->store_id])->column('file_id');

        $where = [

            'uniacid'  => $this->_uniacid,

            'folder_id'=> $input['folder_id'],

            'status'   => 1

        ];

        $map[] = ['is_company','=',1];

        $map[] = ['id','in',$file_id];

        $data = $this->file_model->folderOrFileList($where,$dis,$map,$this->getUserId());

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-25 17:40
     * @功能说明：生产二维码
     */

    public function imQr(){

        $input = $this->_param;

        $input['room_key'] = orderCode();

        $input['user_id']  = $this->getUserId();

        $data = longbingCreateWxCode($this->_uniacid,$input,$input['page']);

        if(isset($data['qr_path'])) $data = transImagesOne($data ,['qr_path'] ,$this->_uniacid);

        $coupon_qr = $data['qr_path'];

        $datas['img'] = $coupon_qr;

        $datas['room_key'] = $input['room_key'];

        return $this->success($datas);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-23 16:28
     * @功能说明:演示房间详情
     */
    public function imRoomList(){

        $input = $this->_param;

        $im_model = new BrochureIm();

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'a.room_key'=> $input['room_key'],

            'a.leader'  => 0,
        ];

        $data = $im_model->dataPageList($dis);

        $file_model = new File();

        $data['radio_file'] = $file_model->dataInfo(['room_key'=>$input['room_key'],'file_type'=>6]);

        if(!empty($data['radio_file'])){

            $data['radio_file']['update_time'] = date('m-d H:i',$data['radio_file']['update_time']);

            $data['radio_file']['file_max'] = getRangeMemKb(0,$data['radio_file']['file_max']);
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-23 16:35
     * @功能说明:判断是否还有没有完成的演示
     */
    public function isImIng(){

        $im_model = new BrochureIm();

        $dis = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'leader'  => 1,

            'status'  => 2

        ];

        $data = $im_model->dataInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-10 14:53
     * @功能说明:雷达每一页的统计
     */
    public function radarPage(){

        $input = $this->_input;

        $count_model = new CardCount();

        $im_page_model = new ImPage();

        $radar = $count_model->where(['id'=>$input['radar_id']])->find()->toArray();

        $dis = [

            'uniacid'     => $this->_uniacid,

            'type'        => $input['type'],

            'brochure_id' => $input['brochure_id'],

            'link'        => $input['first_link'],

            'radar_id'    => $input['radar_id'],

            'user_id'     => $this->getUserId()
        ];

        $first_page = $im_page_model->dataInfo($dis);

        if(empty($first_page)){

            $im_page_model->dataAdd($dis);

        }else{

            $time_long = time()-$first_page['update_time'];

            $first_page['time_long']+=$time_long;

            $im_page_model->dataUpdate(['id'=>$first_page['id']],['time_long'=>$first_page['time_long']]);

        }
        //翻页
        if(!empty($input['next_link'])){

            $dis['link'] = $input['next_link'];

            $next_page = $im_page_model->dataInfo($dis);

            if(empty($next_page)){

                $im_page_model->dataAdd($dis);

            }

        }
        //修正一下雷达的浏览时长(说明上报结束) 因为接口分开请求原因 会有误差
        if(!empty($radar['duration'])){
            //获取总的浏览时长
            $time_long = $im_page_model->where(['radar_id'=>$radar['id']])->sum('time_long');

            $count_model->where(['id'=>$radar['id']])->update(['duration'=>$time_long]);

        }

        return $this->success(true);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-08-16 18:12
     * @功能说明:保存录音文件
     */
    public function addVideo(){

        $input = $this->_input;

        $url      = $input['url'];

        $room_key = $input['room_key'];

        $im_model = new BrochureIm();
        //合并录音
        $im_model->addAudio($room_key,$url);

        return $this->success(true);

    }


    /**
     * @param $ws
     * @param $data
     * @param $fd
     * @功能说明:保存音频
     * @author chenniang
     * @DataTime: 2021-08-10 17:25
     */
    public function addVideoSub(){

        $input = $this->_input;

        $room_key = $input['room_key'];

        $uniacid  = $this->_uniacid;

        $video    = FILE_UPLOAD_PATH.$input['video'];

        if(!empty($input['video'])){

            $im_video_model = new ImVideo();

            $insert = [

                'uniacid' => $uniacid,

                'room_key' => $room_key,

                'video'    => $video,

            ];

            $im_video_model->dataAdd($insert);

        }

        return $this->success(true);


    }












}
